package cn.com.nes.site.action.newmall;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.SMSUtil;
import cn.com.nes.common.util.ValidCodeUtil;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.sms.VerCodeReq;
import cn.com.nes.site.module.service.UserService;
import cn.com.nes.site.module.service.newmall.NewmallService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("newmall")
@RequestMapping("/newmall")
public class NewmallAction {

    @Resource
    private NewmallService service;

    @Resource
    private UserService userService;

    //免登陆直接获取key
    @RequestMapping(method = RequestMethod.POST, value = "/getAgentAndShopkey")
    public @ResponseBody  PageData getAgentAndShopkey(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        String uid = (String)req.getReqData().get("uid") ;
        if(StringUtils.isEmpty(uid)) {
            res.setRetCode(9);
            res.setRetMessage("用户不能为空！");
            return res ;
        }
        res = service.getAgentAndShopkey(req) ;
        return res;
    }

    //获取验证码
    @RequestMapping(method = RequestMethod.POST, value = "/sendValidCode")
    public @ResponseBody
    PageData sendValidCode(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        String phone = (String) req.getReqData().get("phone");
        //验证手机号的唯一性
        String validCode = ValidCodeUtil.generateCode(phone);
        VerCodeReq sms = SMSUtil.sendSMS_ALIYUN_REG(phone, validCode);
        if (sms != null){
            userService.InsertSmsCode(sms);
            res.setRetCode(0);
            res.setRetMessage("获取验证码成功！");
        }else{	res.setRetCode(9);
            res.setRetMessage("获取验证码失败");
        }
        return res;
    }

    //注册登录
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public @ResponseBody  PageData login(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        String code = (String)req.getReqData().get("verCode") ;
        String phone = (String)req.getReqData().get("phone") ;
        if(StringUtils.isEmpty(phone)) {
            res.setRetCode(9);
            res.setRetMessage("请输入手机号！");
            return res ;
        }
        if(!StringUtils.isEmpty(code)) {
            res=service.checkVerCodeIncreaseTime(phone, code);
            if(res.getRetCode()!=0) {
                return res ;
            }
        }
        res = service.login(req) ;
        return res;
    }

    //查询店面信息,店面信息在商城端
    @RequestMapping(method = RequestMethod.POST, value = "/getShopCompanyList")
    public @ResponseBody  PageData getShopCompanyList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getShopCompanyList(req) ;
        return res;
    }

    //查询最近访问门店
    @RequestMapping(method = RequestMethod.POST, value = "/getBrowseCompanyList")
    public @ResponseBody  PageData getBrowseCompanyList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getBrowseCompanyList(req) ;
        return res;
    }

    //查询店面详细信息
    @RequestMapping(method = RequestMethod.POST, value = "/getShopCompanyById")
    public @ResponseBody  PageData getShopCompanyById(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getShopCompanyById(req) ;
        return res;
    }

    // 用户点赞
    @RequestMapping(method = RequestMethod.POST, value = "/userPraise")
    @ResponseBody
    public PageData userPraise(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.userPraise(req) ;
        return res;
    }

    // 用户评论
    @RequestMapping(method = RequestMethod.POST, value = "/userComment")
    @ResponseBody
    public PageData userComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.userComment(req) ;
        return res;
    }

    // 删除用户评论
    @RequestMapping(method = RequestMethod.POST, value = "/deleteUserComment")
    @ResponseBody
    public PageData deleteUserComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.deleteUserComment(req) ;
        return res;
    }

    // 获取用户评论列表
    @RequestMapping(method = RequestMethod.POST, value = "/getUserComment")
    @ResponseBody
    public PageData getUserComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getUserComment(req) ;
        return res;
    }

    // 获取二级用户评论列表
    @RequestMapping(method = RequestMethod.POST, value = "/getUserCommentSecond")
    @ResponseBody
    public PageData getUserCommentSecond(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getUserCommentSecond(req) ;
        return res;
    }

    //查询主页商品推荐
    @RequestMapping(method = RequestMethod.POST, value = "/getProductRecommend")
    public @ResponseBody  PageData getProductRecommend(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getProductRecommend(req) ;
        return res;
    }

    //查询主页商品信息
    @RequestMapping(method = RequestMethod.POST, value = "/getProductList")
    public @ResponseBody  PageData getProductList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getProductList(req) ;
        return res;
    }

    // 新增浏览记录
    @RequestMapping(method = RequestMethod.POST, value = "/addBrowse")
    @ResponseBody
    public PageData addBrowse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.addBrowse(req) ;
        return res;
    }

    //查询主页商品详情
    @RequestMapping(method = RequestMethod.POST, value = "/getProductDetails")
    public @ResponseBody  PageData getProductDetails(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getProductDetails(req) ;
        return res;
    }

    // 商品收藏
    @RequestMapping(method = RequestMethod.POST, value = "/addRelation")
    @ResponseBody
    public PageData addRelation(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.addRelation(req) ;
        return res;
    }

    //收藏列表
    @RequestMapping(method = RequestMethod.POST, value = "/getRelationList")
    public @ResponseBody  PageData getRelationList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getRelationList(req) ;
        return res;
    }

    //查询个人信息
    @RequestMapping(method = RequestMethod.POST, value = "/getUserDals")
    public @ResponseBody  PageData getUserDals(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getUserDals(req) ;
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/getAreaCodeByLocation")
    public @ResponseBody  PageData getAreaCodeByLocation(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq() ;
        res = service.getAreaCodeByLocation(req) ;
        return res ;
    }

    //获取下級行政區劃
    @RequestMapping(method = RequestMethod.POST, value = "/getNextXzqh")
    public @ResponseBody  PageData getNextXzqh(HttpServletRequest request,HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getNextXzqh(req) ;
        res.setRetCode(0) ;
        res.setRetMessage("OK") ;
        return res ;
    }

}
